﻿/*
 * 创建者:  ideapadqiao\qiaojianhui
 * 邮件:    qiaojianhui@cont.com.cn
 * 功能描述: 
 * 历史记录: created by qiaojianhui 2012/2/26 12:26:09  
 *           
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;

namespace oqis.core.dbaccess
{
    /// <summary>
    /// 继承基类OQISDBAccessBase
    /// </summary>
    public class MSAccessAccess : OQISDBAccessBase
    {
        #region 类初始化与构建

        /// <summary>
        /// 类构建
        /// </summary>
        public MSAccessAccess(string dbFile, string password, qiaoMSAccessVersion accessVersion) { CreateAccessConnection(dbFile, password, accessVersion); }

        /// <summary>
        /// 类构建
        /// </summary>
        public MSAccessAccess(string dbFile, qiaoMSAccessVersion accessVersion) { CreateAccessConnection(dbFile, "", accessVersion); }

        /// <summary>
        /// 类构建,默认无密码，且是Access03的mdb数据库
        /// </summary>
        public MSAccessAccess(string dbFile) { CreateAccessConnection(dbFile, "", qiaoMSAccessVersion.Access2003); }

        /// <summary>
        /// 类构建
        /// </summary>
        public MSAccessAccess(string dbFile, string password) { CreateAccessConnection(dbFile, password, qiaoMSAccessVersion.Access2003); }

        #endregion

        #region 类内部方法

        /// <summary>
        /// 创建数据库连接
        /// </summary>
        /// <param name="dbFile">数据库文件</param>
        /// <param name="password">密码</param>
        /// <param name="accessVersion">Access版本</param>
        void CreateAccessConnection(string dbFile, string password, qiaoMSAccessVersion accessVersion)
        {
            string connString, provider = "";
            switch (accessVersion)
            {
                case qiaoMSAccessVersion.Unknow:
                case qiaoMSAccessVersion.Access2003: provider = "Microsoft.Jet.OLEDB.4.0"; break;
                case qiaoMSAccessVersion.Access2007: provider = "Microsoft.ACE.OLEDB.12.0;"; break;
            }
            if (string.IsNullOrEmpty(password))
                connString = string.Format("Provider={0};Data Source={1};Persist Security Info=False;", provider, dbFile);
            else
                connString = string.Format("Provider={0};Data Source={1};Jet OLEDB:Database Password={2};", provider, dbFile, password);

            //创建数据库连接对象
            this._dbConnection = new OleDbConnection(connString);
            this._dbCommand = this._dbConnection.CreateCommand();
            this._dbAdapter = new OleDbDataAdapter((OleDbCommand)_dbCommand);

        }

        #endregion

    }
}
